$(document).ready(function () {
	//数值、数值字符串  装换成货币格式函数
	Number.prototype.formatMoney = function (places, symbol, thousand, decimal) {
		places = !isNaN(places = Math.abs(places)) ? places : 2;
		symbol = symbol !== undefined ? symbol : "";
		thousand = thousand || ",";
		decimal = decimal || ".";
		var number = this,
			negative = number < 0 ? "-" : "",
			i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
			j = (j = i.length) > 3 ? j % 3 : 0;
		return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
	};

	//获取普通积分余额
	get_remaining_sum();
	function get_remaining_sum() {
		$.ajax({
			url: '/user/center',
			type:'POST',
			dataType: 'json',
			data: {
				type: '1'
			},
			success:function (data) {
				if( data.code == '1' ){
					$('.remaining_sum_num').text( ( parseFloat( data.data.user_score ) ).formatMoney() );
				}
			},
			error:function (err) {
				console.log(err);
			}
		})
	}

	//输入兑换数值
	input_conver_num();
	function input_conver_num() {
		var convert_num = $('#convert_num');

		convert_num.on('keyup',function () {
			var convert_form = $('#convert_form');//form表单
			//输入数值只能为正整数
			$(this).val( $(this).val().replace(/[^\d]/g,'') );

			if( $(this).val() >= 100 ){
				if( $(this).val() % 100 == 0 ){
					$(this).val( $(this).val() );

					convert_form.find('input[name="score"]').val( $(this).val() );//form表单赋值
				}
			}

			count_conver_num( $('#status').find('.active').text() );//计算兑换后的金额
		})
	}

	//选择兑换类型
	select_conver_status();
	function select_conver_status() {
		var status = $('#status');
		status.delegate('.pull-right','click',function () {
			status.find('.active').removeClass('active');
			$(this).addClass('active');

			count_conver_num( $(this).text() );//计算兑换后的金额
		})
	}
	
	//计算兑换后的金额
	function count_conver_num(status_text) {
		var convert_num = $('#convert_num');
		var convert_money = $('#convert_money');
		var convert_form = $('#convert_form');//form表单

		if( status_text == '黄金积分' ){
			convert_form.find('input[name="type"]').val( '3' );
			convert_money.text( '兑换为' + status_text + '积分数值为: ' + parseFloat( convert_num.val() * 0.735 ).toFixed(2) + '' );
		}

		if( status_text == '白金积分' ){
			convert_form.find('input[name="type"]').val( '1' );
			convert_money.text( '兑换为' + status_text + '积分数值为: ' + parseFloat( convert_num.val() * 0.5 ).toFixed(2) + '' );
		}

		if( status_text == '钻石积分' ){
			convert_form.find('input[name="type"]').val( '2' );
			convert_money.text( '兑换为' + status_text + '积分数值为: ' + parseFloat( convert_num.val() * 0.416 ).toFixed(2) + '' );
		}
	}

	//提交兑换  ajax
	post_exchange();
	function post_exchange() {
		var bool = true;
		$('#sure_convert').on('click',function () {
			if( bool ){
				bool = false;
				var convert_num = $('#convert_num');
				if( convert_num.val() && convert_num.val() != '' && convert_num.val() % 100 == 0 && $('#status').find('.active').length > 0 ){
					$.ajax({
						url: '/index/exchange_score',
						type: 'POST',
						dataType: 'json',
						data:$('#convert_form').serialize(),
						success:function (data) {
							bool = true;
							$('#myModal').find('.modal-body').text( data.msg );
							$('#myModal').modal();
							if( data.code == '1' ){
								$('#myModal').on('hide.bs.modal',function () {
									go_back();//返回上一页
								});
							}
						},
						error: function (err) {
							console.log(err);
						}
					});
				}else {
					bool = true;
					input_select_tip();/*未输入兑换数值  或者  未选择兑换类型  提示*/
				}
			}


		});
	}

	//未输入兑换数值  或者  未选择兑换类型  提示
	function input_select_tip() {
		var convert_num = $('#convert_num');

		//未输入兑换数值
		if( !convert_num.val() || convert_num.val() == '' ){
			$('#myModal').find('.modal-body').text('请输入需要兑换的普通积分');
			$('#myModal').modal();

			return;
		}

		//输入兑换数值、输入不是100整数倍
		if( convert_num.val() % 100 != 0 ){
			$('#myModal').find('.modal-body').text('请输入100的整数倍的积分');
			$('#myModal').modal();

			return;
		}

		//未选择兑换类型
		if( $('#status').find('.active').length <= 0 ){
			$('#myModal').find('.modal-body').text('请选择兑换类型');
			$('#myModal').modal();

			return;
		}
	}

	//返回上一页
	function go_back() {
		history.go(-1);
	}

	//点击返回键  返回上一页
	click_back_btn();
	function click_back_btn() {
		$('#go_back').on('click',function () {
			go_back();//返回上一页
		});
	}

});